#pragma once


class CVar;

class CConsole: public IConsole, public IInputListener
{
public:
	CConsole();
	~CConsole();

	//	IConsole
	virtual void Show();
	virtual void Hide();
	virtual void Toggle();
	virtual bool IsVisible() const;
	virtual void Render();

	virtual void AddLine(const char* text);
	virtual void ExecuteString(const char* text);

	virtual void RegisterVariable(const char* name, string& variable, const char* default_value);
	virtual void RegisterVariable(const char* name, float& variable, float default_value);
	virtual void RegisterVariable(const char* name, int& variable, int default_value);
	virtual void UnregisterVariable(const char* name);

	virtual ICVar* GetVariable(const char* name);
	//	---

	//	IInputListener
	virtual void OnInputEvent(const SInputEvent& input_event);
	//	---

private:
	bool RenderLine(const char* text, uint index) const;
	void RenderCaret();

	void RegisterVariable(const char* name, CVar* variable);

	uint GetHeight() const;			//	console height in pixels
	uint GetPageSize() const;		//	page size in lines
	uint GetLinesCount() const;	//	overall lines count

	void Scroll(int delta);
	void ScrollToStart();
	void ScrollToEnd();

	void MoveCaret(int delta);
	void MoveCaretToStart();
	void MoveCaretToEnd();
	void ResetCaretBlinkTime();

	void AddToHistory(const char* text);
	void ScrollHistory(int delta);
	void ResetHistoryPos();

	void PrintCharacter(char c);
	void EraseCharacter();
	void CommitInputLine();

private:
	typedef std::vector<string>				TLines;
	typedef std::map<string, CVar*>	TVariables;

private:
	bool		m_bVisible;

	TLines	m_lines;
	uint		m_line_pos;			//	line scrolling offset

	TLines	m_history;
	uint		m_history_pos;	//	history scrolling offset

	string	m_input_line;
	uint		m_caret_pos;		//	input line caret offset
	float		m_caret_blink_time;

	TVariables	m_variables;
};